package com.lc.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lc.admin.base.PageResponse;
import com.lc.admin.bean.NursingPlan;
import com.lc.admin.bean.NursingProjectPlan;
import com.lc.admin.mapper.NursingPlanMapper;
import com.lc.admin.service.NursingPlanService;
import com.lc.admin.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    NursingPlanMapper nursingPlanMapper;
    @Override
    public PageResponse<NursingPlan> list(Integer page, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(page,pageSize);
        List<NursingPlan> list = nursingPlanMapper.list(name, status);
        Page pageObj = (Page) list;
        PageResponse<NursingPlan> response = new PageResponse<>(pageObj);
        response.setRecords(pageObj.getResult());
        // 修正多表查询的bug，total值不对
        response.setTotal(Long.parseLong(pageObj.getResult().size()+""));
        return response;
    }

    @Transactional
    @Override
    public void save(NursingPlan nursingPlan) {
        Long uid = UserThreadLocal.getUserId();
        nursingPlan.setCreateBy(uid);
        nursingPlanMapper.save(nursingPlan);

        List<NursingProjectPlan> nursingProjectPlans = nursingPlan.getNursingProjectPlans();
        for (NursingProjectPlan nursingProjectPlan : nursingProjectPlans) {
            nursingProjectPlan.setCreateBy(uid);
            nursingProjectPlan.setPlanId(nursingPlan.getId());
        }

        // 保存护理项目列表
        nursingPlanMapper.savePlanAndProjectList(nursingProjectPlans);

        // 计算这些项目组成套餐的实际金额
    }

    @Override
    public NursingPlan findById(Long id) {
        return nursingPlanMapper.findById(id);
    }

    @Override
    public void edit(NursingPlan nursingPlan) {

    }

    @Override
    public void enable(Long id, Integer status) {

    }

    @Override
    public void del(Integer id) {

    }
}
